﻿using Alimentador.CustomControls;
using Alimentador.Foods;
using System;
using System.Collections;
using System.ComponentModel;
using System.IO;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;
using System.Xml.Serialization;
using System.Collections.Generic;

namespace Alimentador
{
    public class MainHelper
    {
        /// <summary>
        /// Outros
        /// </summary>
        public const string MAIN_CATEGORY_LIST_MEUS_ALIMENTOS = "Meus Alimentos";
        /// <summary>
        /// Meus Alimentos
        /// </summary>
        public const string FOOD_CATEGORY_ALIMENTOS = "Alimentos";

        public static string GetEnumDescription(Enum value)
        {
            FieldInfo fi = value.GetType().GetField(value.ToString());

            DescriptionAttribute[] attributes =
                (DescriptionAttribute[])fi.GetCustomAttributes(
                typeof(DescriptionAttribute),
                false);

            if (attributes != null &&
                attributes.Length > 0)
                return attributes[0].Description;
            else
                return value.ToString();
        }

        /// <summary>
        /// Serializes an object of type PersonPatient.
        /// </summary>
        /// <param name="person">PersonPatient</param>
        /// <param name="fileName">string</param>
        public static bool SerializePersonObject(string fileName, PersonPatient person)
        {
            bool result = false;
            using (FileStream fs = new FileStream(fileName, FileMode.Create))
            {
                XmlSerializer xSer = new XmlSerializer(typeof(PersonPatient));
                xSer.Serialize(fs, person);
                result = true;
            }
            return result;
        }

        /// <summary>
        /// Deserializes an object file into an object
        /// </summary>
        /// <param name="fileName">string</param>
        /// <returns>object</returns>
        public static PersonPatient DeSerializePersonObject(string fileName)
        {
            PersonPatient person = null;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    XmlSerializer _xSer = new XmlSerializer(typeof(PersonPatient));
                    person = (PersonPatient)_xSer.Deserialize(fs);
                }
            }
            catch (Exception)
            {
                // TODO add exception to log
            }
            return person;
        }

        /// <summary>
        /// Serializes an object of type PersonPatient.
        /// </summary>
        /// <param name="person">PersonPatient</param>
        /// <param name="fileName">string</param>
        public static void SerializeDaysContainerObject(string fileName, ItemCollection WeekDayList)
        {
            Week week = new Week();
            foreach (WeekDay wd in WeekDayList)
            {
                Day d = new Day();
                d.DayName = wd.Header.ToString();
                
                foreach (MealCtrl mc in wd.GetMealList)
                {
                    Meal m = new Meal();
                    m.MealDescription = mc.MealDescription;
                    
                    foreach(Food f in mc.GetFoodList)
                    {
                        FoodEssencial fde = new FoodEssencial();
                        fde.Name = f.GetName;
                        fde.Quantity = f.GetQuantity;
                        fde.Unit = f.GetUnit;

                        m.AddFood(fde);
                    }

                    d.AddMeal(m);
                }

                week.AddDay(d);
            }

            XmlSerializer serializerW = new XmlSerializer(week.GetType());
            StreamWriter sw = new StreamWriter(fileName);
            serializerW.Serialize(sw, week);
            sw.Close();
        }

        internal static Week DescSerializeDaysContainerObject(string fileName)
        {
            Week week = new Week();

            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    XmlSerializer serializer = new XmlSerializer(week.GetType());
                    week = (Week)serializer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception)
            {
                // TODO add exception to log
            }

            return week;
        }

        /// <summary>
        /// Deserializes an object file into an object
        /// </summary>
        /// <param name="fileName">string</param>
        /// <returns>object</returns>
        public static TabControl DeSerializeDaysContainerObject(string fileName)
        {
            TabControl daysContainer = null;
            try
            {
                using (FileStream fs = new FileStream(fileName, FileMode.Open))
                {
                    XmlSerializer _xSer = new XmlSerializer(typeof(TabControl));
                    daysContainer = (TabControl)_xSer.Deserialize(fs);
                    fs.Close();
                }
            }
            catch (Exception)
            {
                // TODO add exception to log
            }
            return daysContainer;
        }


        public static void OnPrint(Control control)
        {
            control.Measure(new Size(816, 1056));
            control.Arrange(new Rect(new Size(816, 1056)));
            control.UpdateLayout();
            new System.Windows.Controls.PrintDialog().PrintVisual(control, "Tax Loan Form");
        }



        internal static List<MainCategoryList> CompareListAndReturnFinal(List<MainCategoryList> listFromExcel, List<MainCategoryList> listFromXML)
        {
            foreach (MainCategoryList mcl_fromExcel in listFromExcel)
            {
                bool exist_mcl = false;
                foreach(MainCategoryList mcl_fromXML in listFromXML)
                {
                    if (mcl_fromExcel.GetName == mcl_fromXML.GetName)
                    {
                        exist_mcl = true;
                    }
                }

                if (!exist_mcl)
                {
                    listFromXML.Add(mcl_fromExcel);


                }
            }


            //foreach(MainCategoryList mcl_fromXML in listFromXML)
            //{
            //    if (mcl_fromXML.GetName == MAIN_CATEGORY_LIST_MEUS_ALIMENTOS) //Meus alimentos
            //    {
            //        listFromExcel.Add(mcl_fromXML);
            //    }
            //}

            return listFromXML;
        }
    }


    public class Week
    {
        private List<Day> m_DayList = new List<Day>();

        [XmlElement("Day")]
        public List<Day> DayList
        {
            get { return m_DayList; }
            set { m_DayList = value; }
        }

        public Week()
        {
        }

        public void AddDay(Day day)
        {
            m_DayList.Add(day);
        }

    }

    public class Day
    {

        private List<Meal> m_mealList = new List<Meal>();

        [XmlElement("Meal")]
        public List<Meal> MealList
        {
            get { return m_mealList; }
            set { m_mealList = value; }
        }

        private string m_dayName = "";

        public Day()
        {
        }
        public string DayName
        {
            get { return m_dayName; }
            set { m_dayName = value; }
        }

        public void AddMeal(Meal meal)
        {
            m_mealList.Add(meal);
        }
    }

    public class Meal
    {
        private List<FoodEssencial> m_foodList = new List<FoodEssencial>();

        [XmlElement("Food")]
        public List<FoodEssencial> FoodList
        {
            get { return m_foodList; }
            set { m_foodList = value; }
        }

        public Meal()
        {
        }

        public string MealDescription { get; set; }

        public void AddFood(FoodEssencial food)
        {
            m_foodList.Add(food);
        }
    }

    public class FoodEssencial
    {
        public FoodEssencial()
        { 
        }

        public string Name { get; set; }

        public string Quantity { get; set; }

        public string Unit { get; set; }
    }

}

